<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Template extends Base_Controller {

    //编辑模板页
    public function editing() {
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/edit');
    }

    //模板列表页
    public function listing() {
        $where = [
            'department_id'=> $this->session->department_id
        ];
        $this->data['sumRecords'] = $this->template_model->getSum('su_template',$where);
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/list');
    }


    //添加邮件模板
    public function add_template() {
        $redata = $this->httpcode;
        $brief = $this->input->post('brief'); //模板简介
        $subject = $this->input->post('subject'); //邮件主题
        $content = $this->input->post('content'); //邮件正文
        $sort = trim($this->input->post('sort')); //排序
        $is_messy = $this->input->post('ismessy'); //防检测
        $attachment = $this->input->post('attachment'); //附件地址
        $department_id = $this->session->department_id; //部门id
        $create_time = tnow();
        if (!empty($attachment)) {
            $attachment = 'uploads/mail_file/' . $attachment;
        }
        $data = [
            'brief' => $brief,
            'subject' => $subject,
            'content' => $content,
            'sort' => $sort,
            'is_messy' => $is_messy,
            'attachment' => $attachment,
            'create_time' => $create_time,
            'update_time' => $create_time,
            'department_id' => $department_id
        ];
        if (is_numeric($sort)) {
            if ($this->template_model->addTemplate($data)) {
                $redata['errno'] = 666;
                $redata['error'] = '邮件模板添加成功';
            } else {
                $redata['errno'] = 602;
                $redata['error'] = '邮件模板添加失败';
            }
        } else {
            $redata['errno'] = 601;
            $redata['error'] = '数据格式错误';
        }
        echo json_encode($redata);
    }

    //获取邮件模板列表
    public function get_template_list() {
        $redata = $this->httpcode;
        $department_id = $this->session->department_id;
        $page = $this->input->post('page');
        $limit = $this->input->post('limit');
        $page = $page*$limit;
        if (is_numeric($page) && is_numeric($limit)) {
            $template_list = $this->template_model->getTemplateList($page, $limit, $this->session->department_id);
            $redata['list'] = $template_list;
            $redata['page'] = $page;
            $redata['limit'] = $limit;

            if (!empty($template_list)) {
                $redata['templateList'] = $template_list;
                $redata['count'] = count($template_list);
            } else {
                $redata['errno'] = 600;
                $redata['error'] = '暂时没有数据';
            }
        } else {
            $redata['errno'] = 601;
            $redata['error'] = '数据非法';
        }

        echo json_encode($redata);
    }

    //删除邮件模板列表
    public function del_template_list() {
        $redata = $this->httpcode;
        $ids = $this->input->post('ids');
        if (!empty($ids)) {
            if (is_array($ids)) {
                foreach ($ids as $v) {
                    if (!is_numeric(trim($v))) {
                        $redata['errno'] = 602;
                        $redata['error'] = '数据非法';
                    } else {
                        if ($this->template_model->delTemplateList($ids)) {
                            $redata['errno'] = 666;
                            $redata['error'] = '删除成功';
                        } else {
                            $redata['errno'] = 603;
                            $redata['error'] = '删除失败';
                        }
                    }
                }
            } else {
                if (!is_numeric(trim($ids))) {
                    $redata['errno'] = 602;
                    $redata['error'] = '数据非法';
                } else {
                    if ($this->template_model->delTemplateList($ids)) {
                        $redata['errno'] = 666;
                        $redata['error'] = '删除成功';
                    } else {
                        $redata['errno'] = 603;
                        $redata['error'] = '删除失败';
                    }
                }
            }
        } else {
            $redata['errno'] = 601;
            $redata['error'] = '请选择需要删除项';
        }
        echo json_encode($redata);
    }


    //修改防检测
    public function change_template_messy() {
        $redata = $this->httpcode;
        $id = $this->input->post('id'); //模板id
        $is_messy = $this->input->post('ismessy'); //是否开启防检测id

        if (is_numeric(trim($id)) && is_numeric(trim($is_messy))) {
            $data = [
                'is_messy' => $is_messy,
                'update_time' => tnow()
            ];
            if ($this->template_model->changeTemplateType($data, $id)) {
                $redata['errno'] = 666;
                $redata['error'] = '防检测功能切换成功';
            } else {
                $redata['errno'] = 603;
                $redata['error'] = '防检测功能切换成功';
            }
        }

        echo json_encode($redata);
    }

    //修改邮件模板内容
    public function update_template() {
        $redata = $this->httpcode;
        $id = $this->input->post('id');
        $subject = $this->input->post('subject');
        $content = $this->input->post('content');
        $brief = $this->input->post('brief');
        $sort = $this->input->post('sort');
        $attachment = $this->input->post('attachment');
        if (is_numeric(trim($sort)) && is_numeric(trim($id))) {
            $data = [
                'subject' => $subject,
                'content' => $content,
                'brief' => $brief,
                'sort'=>$sort,
                'update_time'=> tnow()
            ];
            if(!empty($attachment))
                $data['attachment'] = 'uploads/mail_file/'.$attachment;
            
            
            if ($this->template_model->changeTemplateType($data, $id)) {
                $redata['errno'] = 666;
                $redata['error'] = '修改模板成功';
            } else {
                $redata['errno'] = 603;
                $redata['error'] = '修改模板失败';
            }
        } else {
            $redata['errno'] = 601;
            $redata['error'] = '数据非法';
        }
        echo json_encode($redata);
    }
    
    //获取模板
    public function get_template(){
        $redata = $this->httpcode;
        $department_id = $this->session->department_id;
        $where = [
            'department_id' => $department_id,
        ];
        $templatelist = $this->template_model->getTemplate($where);
        if(!empty($templatelist)) {
            $redata['errno'] = 666;
            $redata['error'] = '获取模板列表成功';
            $redata['templatelist'] =$templatelist;
        } else {
            $redata['errno'] = 601;
            $redata['error'] = '当前分类没有添加模板';
        }
        echo json_encode($redata);
    }
    
    public function get_template_byid(){
        $redata = $this->httpcode;
        $id = $this->input->get('id');
        $where = [
            'id' => $id,
        ];
        $template = $this->template_model->getTemplateByid($where);
        if(!empty($template)) {
            $redata['errno'] = 666;
            $redata['error'] = '获取模板成功';
            $redata['template'] =$template;
        } else {
            $redata['errno'] = 601;
            $redata['error'] = '获取模板失败';
        }
        echo json_encode($redata);
    }
    

}
